<?php


namespace app\model\coach;


use app\model\config\RecycleModel;
use app\model\school\SchoolModel;
use think\Model;
use think\model\concern\SoftDelete;

class UserCoachModel extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $name = 'user_coach';
    protected $autoWriteTimestamp = true;

    protected $hidden = ['password', 'update_time', 'delete_time'];


    public function setPasswordAttr($value)
    {
        if (!$value) {
            return '';
        }
        return password_hash($value, PASSWORD_DEFAULT);
    }

    public function school()
    {
        return $this->hasOne(SchoolModel::class, 'id', 'school_id')->bind(['school_name' => 'name']);
    }

    public static function onBeforeDelete(Model $model)
    {
        $recycleModel = new RecycleModel();
        $data = [
            'uid' => $model->id,
            'type' => 2,
            'title' => $model['username'],
            'mobile' => $model['mobile'],
        ];
        $recycleModel->save($data);
    }
}